/******************************************************************************
 * Copyright (C) ShenZhen Powerdata Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳市博安达信息技术股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.szboanda.business.relevance.config.service.impl;

import com.szboanda.beijing.pure.BaseBusinessService;
import com.szboanda.business.relevance.config.dao.RelevanceConfigDAO;
import com.szboanda.business.relevance.config.exception.RelevanceConfigException;
import com.szboanda.business.relevance.config.service.IRelevanceConfigService;
import com.szboanda.platform.common.component.datahelper.DataHelper;
import com.szboanda.platform.common.framext.mybatits.PlatformPageInfo;
import com.szboanda.platform.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @title: 关联引擎业务配置_业务处理service实现类
 * @fileName: RelevanceConfigServiceImpl.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: 原晋川
 * @date: 2019-09-20 14:51
 * @version: V1.0
 */
@Service("RelevanceConfigService")
public class RelevanceConfigServiceImpl extends BaseBusinessService implements IRelevanceConfigService {

    /**
     * 注入业务处理DAO
     */
    @Autowired
    private RelevanceConfigDAO relevanceConfigDAO;

    /**
     * 查询【关联引擎业务配置】业务信息
     *
     * @param searchInfo 业务Map集合
     * @return 返回业务集合
     * @throws RelevanceConfigException
     */
    @Override
    public List<Map<String, Object>> queryRelevanceConfigs(Map<String, Object> searchInfo) throws RelevanceConfigException {
        try {
            List<Map<String, Object>> list = relevanceConfigDAO.queryRelevanceConfigs(searchInfo);

            return list;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "queryRelevanceConfigs,an exception occured[searchInfo=" + searchInfo + "]", e);
            throw new RelevanceConfigException(e);
        }
    }

    /**
     * 单条查询
     *
     * @param xh 主键
     * @return
     * @throws RelevanceConfigException
     */
    @Override
    public Map<String, Object> queryRelevanceConfigByXh(String xh) throws RelevanceConfigException {
        try {
            if (StringUtils.isEmpty(xh)) {
                return null;
            }
            Map<String, Object> searchInfo = new HashMap<String, Object>();
            searchInfo.put("XH", xh);
            Map<String, Object> model = relevanceConfigDAO.queryRelevanceConfigByXh(searchInfo);
            if (CollectionUtils.isEmpty(model)) {
                return null;
            }
            return model;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "queryRelevanceConfigByXh,an exception occured[xh=" + xh + "]", e);
            throw new RelevanceConfigException(e);
        }
    }

    /**
     * 分页查询【关联引擎业务配置】业务信息
     *
     * @param pageNum    分页数
     * @param pageSize   分页长度
     * @param searchInfo 业务Map集合
     * @return 返回业务集合
     * @throws RelevanceConfigException
     */
    @Override
    public PlatformPageInfo<Map<String, Object>> queryRelevanceConfigsInPage(int pageNum, int pageSize, Map<String, Object> searchInfo) throws RelevanceConfigException {
        try {
            DataHelper.startPage(pageNum, pageSize);
            // 排序处理
            List<Map<String, Object>> orders = (List<Map<String, Object>>) searchInfo.get("order");
            StringBuffer orderByStr = new StringBuffer();
            if (CollectionUtils.isNotEmpty(orders)) {
                for (Map<String, Object> order : orders) {
                    String field = MapUtils.getString(order, "field");
                    String direction = MapUtils.getString(order, "direction");
                    orderByStr.append(field).append(" ").append(direction);
                }
            }
            DataHelper.startPage(pageNum, pageSize, orderByStr.toString());
            List<Map<String, Object>> results = relevanceConfigDAO.queryRelevanceConfigs(searchInfo);
            return new PlatformPageInfo<Map<String, Object>>(results);

        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "查询关联引擎业务配置列表异常", e);
            throw new RelevanceConfigException("查询关联引擎业务配置列表异常", e);
        }
    }

    /**
     * 保存【关联引擎业务配置】业务信息
     *
     * @param model 业务Map集合
     * @return 返回业务集合
     * @throws RelevanceConfigException
     */
    @Override
    public Map<String, Object> saveRelevanceConfig(Map<String, Object> model) throws RelevanceConfigException {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = 0;
            super.createBaseInfo(model);
            if (isExist(model)) {
                count += relevanceConfigDAO.updateRelevanceConfig(model);
            } else {
                if ("".equals(MapUtils.getString(model, "XH_TEMP"))) {
                    model.put("XH", Toolkit.getID());
                } else {
                    model.put("XH", model.get("XH_TEMP"));
                }
                count += relevanceConfigDAO.addRelevanceConfig(model);
            }
            //result.setResult(this.getSaveResult(MapUtils.getString(model, "XH"), count));
            result.put("count", count);
            return result;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "saveRelevanceConfig,an exception occured[model=" + model + "]", e);
            throw new RelevanceConfigException(e);
        }
    }

    /**
     * 删除【关联引擎业务配置】业务信息
     *
     * @param infos 业务Map集合
     * @return 返回操作标识符
     * @throws RelevanceConfigException
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deleteRelevanceConfig(Map<String, Object> infos) throws RelevanceConfigException {
        try {
            List<String> delXhs = new ArrayList<String>();
            List<Map<String, Object>> modelInfos = (List<Map<String, Object>>) infos.get("models");
            for (Map<String, Object> modelInfo : modelInfos) {

                delXhs.add(MapUtils.getString(modelInfo, "XH"));
            }

            Map<String, Object> info = new HashMap<String, Object>();
            info.put("delXHs", delXhs);
            relevanceConfigDAO.deleteRelevanceConfigByXhs(info);
            return new HashMap<>();
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "deleteRelevanceConfig,an exception occured[infos=" + infos + "]", e);
            throw new RelevanceConfigException(e);
        }
    }

    /**
     * 按主键删除【关联引擎业务配置】业务信息
     *
     * @param xh 主键
     * @return 返回操作标识符
     * @throws RelevanceConfigException
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deleteRelevanceConfigByXh(String xh) throws RelevanceConfigException {
        try {
            return new HashMap<>();
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), "deleteRelevanceConfigByXh,an exception occured[xh=" + xh + "]", e);
            throw new RelevanceConfigException(e);
        }
    }

    /**
     * 判断【关联引擎业务配置】业务信息是否存在
     *
     * @param model 业务Map集合
     * @return 返回操作标识符
     * @throws RelevanceConfigException
     */
    private boolean isExist(Map<String, Object> model) {
        String xh = MapUtils.getString(model, "XH");
        if ("".equals(xh)) {
            return false;
        } else if (null == this.queryRelevanceConfigByXh(xh)) {
            return false;
        }
        return true;
    }


}
